package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public Result list(UserDto userdto){
        log.info("查询用户");
        PageResult<User> pageResult = userService.list(userdto);
        return Result.success(pageResult);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }

    @Log
    @DeleteMapping("/{ids}")
    public Result deleteById(@PathVariable List<Integer> ids){
        log.info("删除用户 id{}" + ids);
        userService.deleteById(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        User user = userService.getById(id);
        return Result.success(user);
     }

    @Log
    @PostMapping
    public Result add(@RequestBody User user){
        userService.add(user);
        return Result.success();
    }

    @GetMapping("/list")
    public Result all(){
        List<User> list = userService.all();
        return Result.success(list);
    }

    @GetMapping("/role/{roleLabel}")
    public Result getByRole(@PathVariable String roleLabel){
        log.info("根据角色查询用户");
        List<User> list = userService.getByRole(roleLabel);
        return Result.success(list);
    }

    @GetMapping("/dept/{deptId}")
    public Result getByDept(@PathVariable Integer deptId){
        log.info("根据部门查询用户");
        List<User> list = userService.getByDept(deptId);
        return Result.success(list);
    }
}
